package com.softeware.control;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import com.softeware.pojo.UserInfo;
import com.softeware.service.UserService;
import com.softeware.service.impl.UserServiceImpl;

import java.io.IOException;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    private UserService userService;
    @Override
    public void init() throws ServletException {
        ServletContext context = getServletContext();
        userService = new UserServiceImpl(context);
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserInfo user = new UserInfo(username,password);
        if("login".equals(action)){
            UserInfo loggedUser = userService.login(username,password);
            if(loggedUser!=null){
                //计算访客数量
                visitorContext(request);
                request.getSession().setAttribute("username",username);
                request.getRequestDispatcher("main.jsp").forward(request,response);
            }else{
                request.getRequestDispatcher("Login.jsp?info=unLogin").forward(request,response);
            }
        }
        if("register".equals(action)){
            if(userService.register(user)){
                //计算访客数量
                visitorContext(request);
                request.getSession().setAttribute("username",username);
                request.getRequestDispatcher("main.jsp").forward(request,response);
            }else {
                request.getRequestDispatcher("Login.jsp?info=unRegister").forward(request,response);
            }
        }
        if("logout".equals(action)){
            LogOut(request,response);
        }
    }
        private void visitorContext(HttpServletRequest request){
            Integer visitors = (Integer) getServletContext().getAttribute("visitors");
            if(visitors == null){
                visitors = 1;
            }else{
//                if(request.getSession(false).isNew())
                    visitors+=1;
            }
            getServletContext().setAttribute("visitors",visitors);
        }
        private void LogOut(HttpServletRequest request,HttpServletResponse response) throws IOException {
            HttpSession session = request.getSession(false);
            if (session != null) {
                // 销毁会话
                session.invalidate();
            }

            // 创建一个JSESSIONID Cookie并设置其最大年龄为0，即删除它
            Cookie cookie = new Cookie("JSESSIONID", null); // 名称为JSESSIONID
            cookie.setMaxAge(0); // 设置Cookie立即失效
            cookie.setPath("/"); // 确保覆盖所有路径
            response.addCookie(cookie);

            // 重定向到登录页面或其他页面
            response.sendRedirect("main.jsp");
        }
}